import { _decorator, Component , Label, UIOpacity, Vec3 , tween} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Toast')
export class Toast extends Component {
    @property(Label)
    label: Label = null;

    show(message: string, duration: number = 2) {
        this.label.string = message;

        const opacity = this.getComponent(UIOpacity);
        opacity.opacity = 0;
        this.node.setScale(new Vec3(1, 1, 1));

        tween(opacity)
            .to(0.2, { opacity: 255 })       // 淡入
            .delay(duration)                 // 停留
            .to(0.3, { opacity: 0 })         // 淡出
            .call(() => {
                this.node.destroy();         // 销毁节点
            })
            .start();
    }
}